home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gwuada_9.zip / LIB.C < prev    next >
C/C++ Source or Header  |  1993-07-27  |  18KB  |  703 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9. #include "hdr.h"
  10. #include "vars.h"
  11. #include "libhdr.h"
  12. #include "ifile.h"
  13. #include "dbxp.h"
  14. #include "miscp.h"
  15. #include "smiscp.h"
  16. #include "setp.h"
  17. #include "libfp.h"
  18. #include "librp.h"
  19. #include "libp.h"
  20.  
  21. static char *update_lib_maps_remove(char *, int);
  22. static void sym_restore(Symbol);
  23.  
  24. /* keeping unit_nodes as tuple, unit_nodes_now is number of actual elements */
  25. void unit_nodes_add();
  26.  
  27. /*
  28.  *   Procedures in this module serve two phases of the compiler:
  29.  *
  30.  *     (1)  maintaining a program library during semantic translation,
  31.  *
  32.  *     (2)  reading in and writing out the intermediate files associated with
  33.  *        semantic processing.
  34.  *
  35.  *   Three types of files are used here:
  36.  *
  37.  *    AIS files    information generated during the translation of a source 
  38.  *             file,
  39.  *
  40.  *    TRE files    intermediate code
  41.  *
  42.  *    LIB files    directory to units in AIS files,
  43.  *
  44.  *    LIB files and AIS files are each organized as a pair of maps whose
  45.  *    domain elements are unique compilation unit names such as:
  46.  *
  47.  *    ['subprog spec', 'MAIN']
  48.  *
  49.  *    ['spec', 'MATH_PACK']
  50.  *
  51.  *    ['subprog', 'SIN', 'MATH_PACK']
  52.  *
  53.  *   The first string in these names is gives the unit's class as seen
  54.  *   by the binder:
  55.  *
  56.  *    'subprog spec', 'subprog'  -- subprogram specifications & bodies
  57.  *
  58.  *    'spec', 'body'    -- package specifications & module bodies
  59.  *
  60.  *   The second string is the name of the compilation unit itself.  If
  61.  *   this is a subunit, the remaining names are those of its enclosing
  62.  *   scopes.
  63.  *
  64.  *   A LIB file is a pair of maps from these unique names to the
  65.  *   appropriate AIS files names:
  66.  *
  67.  *    (1)  LIB_UNIT, which indicates the file containing the
  68.  *         translation of each compilation unit, and
  69.  *
  70.  *    (2)  LIB_STUB, which indicates the file containing the
  71.  *         translation of the stub of the subunit.
  72.  *
  73.  *   Each AIS file is a parallel pair of maps, again from unique names,
  74.  *   containing the translation of each compilation unit and the
  75.  *   environment of each stub.    For convenience, these two maps are split
  76.  *   into five within the translator itself:
  77.  *
  78.  *   COMP_DATE
  79.  *    A map for each compilation unit to compilation dates & times
  80.  *    checking consistency).    Dates themselves are a tuple including
  81.  *    the date and clock time of translation, and an indication of
  82.  *    the order of compilation within a single session.
  83.  *
  84.  *   PRE_COMP
  85.  *    List of units that should have been compiled before this one.
  86.  *
  87.  *   UNIT_DECL
  88.  *    The declarations that can be seen by other units, or that will
  89.  *    be needed later by the translator.
  90.  *
  91.  *   STUB_ENV
  92.  *    The environment at the point where the stub was declared.
  93.  *
  94.  *   During the initialization of the compiler, several predefined
  95.  *   library units are read in and permanently installed.  These units
  96.  *   are not included explicitly in the library, but may be accessed
  97.  *   as if they were.  The information from their AIS files is stored in
  98.  *   the map 'predef_map' (local to this module), and a set of those
  99.  *   currently available (not displaced by a user's unit) is maintained
  100.  *   in the global variable PREDEF_UNITS.  For simplicity, these
  101.  *   predefined units may not have stubs.
  102.  *
  103.  *   The semantic analyser has access to compilation information in the
  104.  *   AIS files through the procedures RETRIEVE and STUB_RETRIEVE.  When
  105.  *   called, these two procedures try to make the requested information
  106.  *   available in the five compilation maps listed above (it may read
  107.  *   an AIS file, copy from predef_map or the information may
  108.  *   already be present). If successful, they return the value TRUE,
  109.  *   otherwise they return FALSE.
  110.  *
  111.  *   UPDATE_LIB_MAPS is called to do some housekeeping when a new 
  112.  *   compilation unit is started.
  113.  *
  114.  *   The user may choose to not use the separate compilation facility
  115.  *   and put every compilation unit into one file.  In this case,
  116.  *   the LIB file can be omitted, since its role is to group several
  117.  *   AIS files together.  Furthermore, since AIS files contain all of
  118.  *   the information produced by a translation session, more than
  119.  *   one LIB file may refer to a single AIS file.
  120.  */
  121.  
  122.  
  123.  
  124. extern IFILE *LIBFILE;
  125.  
  126. int init_predef()                /*;init_predef*/
  127. {
  128.     char *lname;
  129.     char *t_name;
  130.     extern char *PREDEFNAME;
  131.  
  132.     lname = libset(PREDEFNAME); /* set PREDEF library as library */
  133.     LIBFILE = ifopen("predef", "lib", "r", 0);
  134.     t_name =libset(lname); /* restore prior library */
  135.     return(read_lib());    /* number of units read */
  136. }
  137.  
  138. char *predef_unit_name(int i)                            /*;predef_unit_name*/
  139. {
  140.     static    char *predef_unit_names[15] = { "",
  141.      "spSYSTEM", "spIO_EXCEPTIONS", "spSEQUENTIAL_IO", 
  142.      "boSEQUENTIAL_IO", "spDIRECT_IO", "boDIRECT_IO", 
  143.       "spTEXT_IO", "boTEXT_IO", "spCALENDAR", "boCALENDAR",
  144.       "ssUNCHECKED_DEALLOCATION", "suUNCHECKED_DEALLOCATION",
  145.       "ssUNCHECKED_CONVERSION", "suUNCHECKED_CONVERSION"};
  146.     return predef_unit_names[i];
  147. }
  148.  
  149. int predef_node_count(int i)                            /*;predef_node_count*/
  150. {
  151.     static int node_count[15] = {0,166, 29, 449, 5, 620, 5, 2654, 17, 470, 5,
  152.       20, 21, 19, 32};
  153.     return node_count[i];
  154. }
  155.  
  156. int predef_symbol_count(int i)                        /*;predef_symbol_count*/
  157. {
  158.     static int symbol_count[15] = {0,31, 13, 61, 0, 88, 0, 409, 1, 83, 1,
  159.       5, 0 ,4 ,1};
  160.     return symbol_count[i];
  161. }
  162.  
  163. int retrieve(char *name)                            /*;retrieve*/
  164. {
  165.     char    *fname;
  166.     /*
  167.      * If the unit 'name' has not previously been read from
  168.      * an AIS file, the file is read and its the unit's contents added
  169.      * to the compilation maps.
  170.      */
  171.  
  172. #ifdef TBSN
  173.     if (getdebug) TO_ERRFILE(strjoin("RETRIEVE ", name));
  174. #endif
  175.     fname = lib_unit_get(name);
  176.     if (fname == NULL) return FALSE;
  177.     if (!streq(fname, AISFILENAME) && !in_aisunits_read(name)){
  178.         if (read_ais(fname, FALSE, name, 0, TRUE) == NULL) { 
  179.             return FALSE;  /* Message emitted by READ_AIS.*/
  180.         }
  181.     }
  182.     return TRUE;
  183. }
  184.  
  185. int last_comp_index(IFILE *ifile)                        /*;last_comp_index*/
  186. {
  187.     /* determine the number of comp units in ifile. */
  188.     long    rec;
  189.     int        i;
  190.  
  191.     i=0;
  192.     for (rec=read_init(ifile); rec!=0; rec=read_next(ifile,rec)) i++; 
  193.     return i;
  194. }
  195.  
  196. int stub_retrieve(char *name)                    /*;stub_retrieve*/
  197. {
  198.     char    *fname;
  199.     Tuple    stubtup, tup;
  200.     int        si, n, i;
  201.  
  202.     /*
  203.      * Reads, if necessary, information from the file in which the stub
  204.      * 'name' was declared.
  205.      */
  206. #ifdef TBSN
  207.     if (putdebug) TO_ERRFILE(strjoin("STUB_RETRIEVE ", name));
  208. #endif
  209.     fname = lib_stub_get(name);
  210.     if (fname == NULL) return FALSE;
  211.     if (!streq(fname, AISFILENAME)) {
  212.         si = stub_numbered(name);
  213.         stubtup = (Tuple) stub_info[si];
  214.         tup = (Tuple) stubtup[4];
  215.         n = tup_size(tup);
  216.         for (i = 1;i <= n; i++) {
  217.              retrieve(pUnits[(int)tup[i]]->name);
  218.         }
  219.         if (!read_stub(fname, name, "st1")) return FALSE;
  220.     }
  221.     return TRUE;
  222. }
  223.  
  224. void update_lib_maps(char *name, char unit_typ)                /*;update_lib_maps*/
  225. {
  226.     char    *uname, *body, *typ, *other, *unit;
  227.     int    i;
  228.  
  229.     /*
  230.      * Add current unit -name- to lib map, and remove references in
  231.      * library maps to previously compiled units with the same name.
  232.      * 
  233.      * The effect of constant map 'remove' in SETL version is achieved
  234.      * in C using procedure update_lib_maps_remove, which is to be
  235.      * found after this procedure.
  236.      */
  237.  
  238.     uname = unit_name_type(name);
  239.     if (unit_typ == 'u') {
  240.         if (streq(uname , "sp") && lib_unit_get(name) != NULL) {
  241.             body = strjoin("bo", unit_name_names(name));
  242.             if (lib_unit_get(body) != NULL)
  243.             lib_unit_put(body, NULL);
  244.         }
  245.     /*
  246.      * If no other units points to the AISCODE in question, remove it
  247.      * from LIB_AIS.  In principle, something analoguous should may be done
  248.      * for systems that allows deletion of a file.
  249.      */
  250.         lib_unit_put(name, AISFILENAME);
  251.         for (i = 1;i <= 2; i++) {
  252.             typ = update_lib_maps_remove(uname, i);
  253.             /*(forall typ in (remove(name(1)) ? {}) )*/
  254.             if (typ == NULL) continue;
  255.             /*other := [typ] + name(2..);*/
  256.             other = strjoin(typ, unit_name_names(name));
  257.             if (lib_unit_get(other) != NULL) {
  258.                 lib_unit_put(other, NULL);
  259.                 empty_unit_slots++;
  260.              }
  261.         }
  262.     }
  263.     else if  (unit_typ == 's') {
  264.         lib_stub_put(name, AISFILENAME);
  265.         i